package light.head.dao.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Resignation;

import org.springframework.stereotype.Service;

@Service
public class ResignationDAO extends BaseDAO<Resignation, Integer> {

	public RESULT findByMap(Integer cu,Map<String, String> map,PageBean pb){
		StringBuffer hql = new StringBuffer("FROM Resignation r WHERE r.cu = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(cu);
		if(null != map) {
			String start = map.get(Parm.START);
			String end = map.get(Parm.END);
			String un = map.get(Parm.UN);
			String oc = map.get(Parm.OC);
			String reason = map.get(Parm.REASON);
			String deleted = map.get(Parm.DELETED);
			if(isNotNull(start)) {
				hql.append(" AND r.ct < ?");
				list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
			}if(isNotNull(end)) {
				hql.append(" AND r.ct > ?");
				list.add(DateUtils.getDateByStr(end, "yyyy-MM-dd"));
			}if(isNotNull(un)) {
				hql.append(" AND r.uc in (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");
				list.add("%" + un + "%");
			}if(isNotNull(oc)) {
				hql.append(" AND r.oc = ?");
				list.add(Integer.parseInt(oc));
			}if(isNotNull(reason)) {
				hql.append(" AND r.reason LIKE ?");
				list.add("%" + reason + "%");
			}if(isNotNull(deleted)) {
				hql.append(" AND r.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY r.code DESC");
		
		if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
	public List<Resignation> findCu(){
		StringBuffer hql = new StringBuffer("FROM Resignation");
		
		return find(hql.toString());
		
	}
	public List<Resignation> findTime(String nt){
		StringBuffer sql = new StringBuffer(" FROM Resignation r WHERE TO_DAYS( r.ft ) - TO_DAYS(?) <= 1 AND r.quit = 0");
		List<Object> parm = new ArrayList<Object>();
		parm.add(nt);
		return find(sql.toString(), parm.toArray());
	}
	
	/**
	 * 审核申请单(查询出待审核的物品申请单：1.上一级审核已经通过的， 2.本机构员工所提交的)
	 * @param ocs	当前机构的跟机构下的所有子机构集合
	 * @param rc	角色编号
	 * @param map	查询条件
	 * @param pb	分页信息
	 */
	public RESULT findByMap_1(List<Integer> ocs, Integer rc, Map<String, String> map , PageBean pb){
		if (null == ocs || null == rc) return null;
		
		List<Object> parm = new ArrayList<Object>();	
		List<Object> countParm = new ArrayList<Object>();
		
		StringBuffer temp = new StringBuffer();
		temp.append(" FROM Resignation a");
		temp.append(" JOIN UserInfo u ON u.code = a.cu");
		temp.append(" JOIN Organ o ON o.code = a.oc");
		temp.append(" JOIN AuditStatus s on s.mc = a.code and s.beforeStatus = 1 WHERE a.deleted = 0 AND (s.business = 5 or s.business = 6) AND s.rc = ? and a.oc IN (");
		
		parm.add(rc);
		countParm.add(rc);
		for (Integer ig : ocs) {
			temp.append("?,");
			parm.add(ig);
			countParm.add(ig);
		}
		
		temp.deleteCharAt(temp.length() - 1).append(")");
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String title = map.get(Parm.TITLE);
			
			if (isNotNull(deleted)) {
				parm.add(Boolean.parseBoolean(deleted));
				countParm.add(Boolean.parseBoolean(deleted));
				temp.append(" AND a.deleted = ?");
			}
			if (isNotNull(time1)) {
				parm.add(DateUtils.getDateByStr(time1, null));
				countParm.add(DateUtils.getDateByStr(time1, null));
				temp.append(" AND a.at >= ?");
			}
			if(isNotNull(time2)){
				parm.add(DateUtils.getDateByStr(time2, null));
				countParm.add(DateUtils.getDateByStr(time2, null));
				temp.append(" AND a.at <= ?");
			}
			if(isNotNull(title)) {
				parm.add("%" + title + "%");
				countParm.add("%" + title + "%");
				temp.append(" AND a.title LIKE ?");
			}
		}	 
		
		StringBuffer sql = new StringBuffer("SELECT a.code, a.reason, a.cu, a.ct, a.oc, a.ft,a.head, u.realName, o.name");
		StringBuffer countSql = new StringBuffer("SELECT COUNT(*)");
		
		sql.append(temp).append(" ORDER BY a.code DESC");
		countSql.append(temp);
		
		if (RESULT.SUCCESS == queryToPageBean(pb, sql.toString(), parm, countSql.toString(), countParm))
			return packageResult(pb);
		else
			return RESULT.ERROR;
		}
	/***
	 * 封装
	 * @param pb
	 * @return
	 */
	private RESULT packageResult(PageBean pb) {
		
		Resignation info = null;
		List<Resignation> list = new ArrayList<Resignation>();
		
		//  a.code, a.title, a.au, a.at, a.oc, a.deleted, u.realName, o.name
		for (Object o : pb.getResultList()) {
			
			info = new Resignation();
			Object[] obj = (Object[]) o;
			
			Integer code = (null == obj[0] || Sys.NONE.equals(obj[0].toString())) ? null : (Integer) obj[0];
			String reason = (null == obj[1] || Sys.NONE.equals(obj[1].toString())) ? null : (String) obj[1];
			Integer cu = (null == obj[2] || Sys.NONE.equals(obj[2].toString())) ? null : (Integer) obj[2];
			Date ct = (null == obj[3] || Sys.NONE.equals(obj[3].toString())) ? null : (Date) obj[3];
			Integer oc = (null == obj[4] || Sys.NONE.equals(obj[4].toString())) ? null : (Integer) obj[4];
			Date ft = (null == obj[5] || Sys.NONE.equals(obj[5].toString())) ? null : (Date) obj[5];
			Boolean head = (null == obj[6] || Sys.NONE.equals(obj[6].toString())) ? null : (Boolean) obj[6];
			String cn = (null == obj[7] || Sys.NONE.equals(obj[7].toString())) ? null : (String) obj[7];
			String on = (null == obj[8] || Sys.NONE.equals(obj[8].toString())) ? null : (String) obj[8];
			
			info.setCode(code);
			info.setReason(reason);
			info.setCu(cu);
			info.setCt(ct);
			info.setOc(oc);
			info.setFt(ft);
			info.setHead(head);
			info.setCn(cn);
			info.setOn(on);
			
			list.add(info);
		}
		pb.setResultList(list);
		return RESULT.SUCCESS;
	}
	//查询所有离职信息
	public RESULT findByMap_1(List<Integer> ocs,Map<String, String> map,PageBean pb){
		StringBuffer hql = new StringBuffer("FROM Resignation r WHERE r.pass = 3 AND r.oc IN (");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if(null != map) {
			String start = map.get(Parm.START);
			String end = map.get(Parm.END);
			String un = map.get(Parm.UN);
			String oc = map.get(Parm.OC);
			String reason = map.get(Parm.REASON);
			String deleted = map.get(Parm.DELETED);
			if(isNotNull(start)) {
				hql.append(" AND r.ct < ?");
				list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
			}if(isNotNull(end)) {
				hql.append(" AND r.ct > ?");
				list.add(DateUtils.getDateByStr(end, "yyyy-MM-dd"));
			}if(isNotNull(un)) {
				hql.append(" AND r.uc in (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");
				list.add("%" + un + "%");
			}if(isNotNull(oc)) {
				hql.append(" AND r.oc = ?");
				list.add(Integer.parseInt(oc));
			}if(isNotNull(reason)) {
				hql.append(" AND r.reason LIKE ?");
				list.add("%" + reason + "%");
			}if(isNotNull(deleted)) {
				hql.append(" AND r.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY r.code DESC");
		
		if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
}
